package com.peg.rules;

import com.peg.dataobjects.Context;
import com.peg.dataobjects.Functor;
import com.peg.dataobjects.FunctorVoid;

/**
 * @author sad
 */
public class Action extends Rule {

    private Functor<Context, Boolean> _function;
    private FunctorVoid<Context> _action;
    private String actionName;

    public Action(Functor<Context, Boolean> function) {
        _function = function;
    }

    public Action(String actionName) {
        this.actionName = actionName;
    }

    public FunctorVoid<Context> getAction() {
        return _action;
    }

    public void setAction(FunctorVoid<Context> _action) {
        this._action = _action;
    }

    public Action(FunctorVoid<Context> action) {
        _action = action;
    }

    @Override
    public boolean execute(Context context) {
        if (_function != null) {
            return _function.execute(context);
        } else {
            _action.execute(context);
            return true;
        }
    }

    @Override
    public String toString() {
        return "Action ["+actionName+"]";
    }
}
